package org.arabidopsis.ahocorasick;

import cn.hutool.core.io.resource.ResourceUtil;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;


public class WordsFilter {
    private static HashMap<String, Integer> keyScore;
    private static AhoCorasick tree;
    private static final String path = "/guolv";

    private static final WordsFilter instance = new WordsFilter();

    private WordsFilter() {
        if (tree == null) {
            String p = ResourceUtil.getResource("guolv").getFile();
            tree = initTree(p);
        }
    }

    public static WordsFilter getInstance() {
        return instance;
    }

    public static boolean forbidden(String str) {
        boolean show = true;
        show = tree.forbidden(str.getBytes(StandardCharsets.UTF_8), keyScore);
        return show;
    }

    public static String filterStr(String str, String b) {
        return tree.forbiddenStr(str.getBytes(StandardCharsets.UTF_8), b);
    }

    public static AhoCorasick initTreeFromReader(BufferedReader reader) {
        AhoCorasick tree1 = new AhoCorasick();
        try {
            String s = reader.readLine();
            String[] tmp = null;
            while (s != null) {
                s = s.toLowerCase().trim();
                tmp = s.split("\\s+");
                if (tmp != null && tmp.length == 2) {
                    tree1.add(tmp[0].getBytes(StandardCharsets.UTF_8), tmp[0]);
                    if (keyScore == null) {
                        keyScore = new HashMap<String, Integer>();
                    }
                    keyScore.put(tmp[0], getScoreFromStr(tmp[1], Integer.valueOf(100)));
                }
                s = reader.readLine();
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        tree1.prepare();
        tree = tree1;
        tree1 = null;
        return tree;
    }

    public static AhoCorasick initTree(String file) {
        AhoCorasick tree1 = null;
        try {
            BufferedReader file1 = new BufferedReader(new InputStreamReader(
                    new FileInputStream(new File(file)), StandardCharsets.UTF_8));
            tree1 = initTreeFromReader(file1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tree1;
    }

    public static Integer getScoreFromStr(String scoreStr, Integer defalut) {
        try {
            return Integer.valueOf(Integer.parseInt(scoreStr));
        } catch (Exception e) {
            return defalut;
        }
    }

    public static String replaceHtml(String html) {
        String str = html.replaceAll("\\p{Space}", "");
        str = str.replaceAll("<script>.*?</script>", "");
        str = str.replaceAll("<style.*?</style>", "");
        str = str.replaceAll("\\&[a-zA-Z]{1,10};", "")
                .replaceAll("<[^>]*>", "");
        str = str.replaceAll("[(/>)<]", "");

        return str;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "你<style>sfasdfsda</style></title><style>html{overflow-y:auto}body{font:12px arial;text-align:center;background:#fff}body,p,form,ul,li{margin:0;padding:0;list-style:none}body,form,#fm{position:relative}td{text-align:left}img{border:0}a{color:#00c}a:active{color:#f60}#u{color:#999;padding:4px 10px 5px 0;text-align:right}#u a{margin:0 5px}#u .reg{margin:0}#m{width:680px;margin:0 auto;}#nv a,#nv b,.btn,#lk{font-size:14px}#fm{padding-left:90px;text-align:left}input{border:0;padding:0}#nv{height:19px;font-size:16px;margin:0 0 4px;text-align:left;text-indent:117px;}.s_ipt_wr{width:418px;height:30px;display:inline-block;margin-right:5px;background:url(</style>好";
        long s = System.currentTimeMillis();
        getInstance();
        boolean b = forbidden(str);
        System.out.println(("每个女孩子都姦染".getBytes(StandardCharsets.UTF_8)).length);
        System.out.println(b);
        str = "你占领外滩占领外滩占领外滩占领外滩好我是你的占领中关村美女";
//        str = "书友111206175549599";
        getInstance();
        str = replaceHtml(str);
        getInstance();
        str = filterStr(str, "*");
        System.out.println(str);

        System.out.println(str);
    }
}